package com.ruoyi.dataMain.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.bill.domain.bo.BillPortBo;
import com.ruoyi.bill.domain.vo.BillPortVo;
import com.ruoyi.bill.domain.vo.BillPortVo;
import com.ruoyi.bill.service.IBillPortService;
import com.ruoyi.bill.service.IBillPortService;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class BillPortImportListener extends AnalysisEventListener<BillPortVo> implements ExcelListener<BillPortVo> {

    private final IBillPortService iBillPortService;
    private final String operName;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public BillPortImportListener() {
        this.iBillPortService = SpringUtils.getBean(IBillPortService.class);
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public void invoke(BillPortVo billPortVo, AnalysisContext context) {
        BillPortBo billPortBo = BeanUtil.toBean(billPortVo, BillPortBo.class);
        try {
            // 验证是否存在这个用户

            ValidatorUtils.validate(billPortBo);
            billPortBo.setCreateBy(operName);
            iBillPortService.insertByBo(billPortBo);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、数据标识 ").append(billPortBo.getId()).append(" 导入成功");
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、数据标识 " +billPortBo.getId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<BillPortVo> getExcelResult() {
        return new ExcelResult<BillPortVo>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<BillPortVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
